import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';
import { Component, OnInit, ViewChild} from '@angular/core';
// import { ToastService, UploadConfig } from 'ng-tui';
import { GlobalService, AuthService } from '../../../../cores/services';
import { SaleService } from '../../services/sale.service';
import { Manage } from '../../interfaces/manage.interface';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, Item } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { Goods } from '../../interfaces/manage.interface';
import { skipWhile, switchMap } from 'rxjs/operators';
import { from } from 'rxjs';

@Component({
    templateUrl: './counter-sales.component.html',
    providers: [Location, { provide: LocationStrategy, useClass: PathLocationStrategy }],
})
export class CounterSalesComponent implements OnInit {
    @ViewChild('tsTabs') tsTabs: any;
    theads = ['#','结算单号','专柜编号', '专柜名称', '所属客户', '业务员','理货员',
        '导购员', '起始日期', '截至日期', '销售金额', '结账日', '交票日', '付款日', '录入人','操作'];
    billTh = ['#', '价格带名称','单位','销售日期','销售数量','零售价格', '零售金额', '标准结算金额',  
         '正常零售价','价格差异','差异原因','操作'];
    orderTh = ['#', '商品编号', '商品名称', '条形码', '厂家货号', '单位', '口味/颜色', '规格/尺寸', '数量', '箱数', '价格', '金额', '税率', '备注', '箱条码', '品牌', '操作'];

    search = new SearchParams({ start: '', end: '' });

    list = [];
    detailList = [];
    loading = false;
    idDetail: any = '';
    isSelect = false;

    pagination = new Pagination();
    parentTypes = new Array<Item>();
    childTypes = new Array<any>();
    goodsThreeType = new Array<any>();
    goods = { id: 0, isActive: 1, goodsName: '', isSelect: false, goodsParentType: '' };


    get getChildTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    get getThreeTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private confirm: ConfirmService,
        private toast: ToastService,
        public global: GlobalService,
        private manageService: SaleService
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/sales/counsales'))
            .subscribe(() => {
                this.loadDatas(() => {
                    this.idDetail = this.list[0].id;
                    this.loadDetailDatas(this.idDetail);
                });
            });
    }
    see() {
        this.tsTabs.changeTab('订单明细');
    }
    back() {
        this.tsTabs.changeTab('订单列表');
    }
    // doSearch() {
    //     this.pagination.reset();
    //     this.loadDatas();
    // }

    // doReset() {
    //     this.search.clean();
    //     this.doSearch();
    // }

    loadDatas(complete = new Function) {
        this.loading = true;
        this.manageService.searchSettlement(this.pagination, this.search)
            .subscribe({
                next: res => {
                    res.datas.rows.forEach(item => {
                        item['isSelect'] = this.isSelect;
                    });
                    this.list = res.datas.rows;
                    console.log(this.list)
                    complete();
                },
                complete: () => this.loading = false
            });
    }


    loadDetailDatas(detailId: number) {
        this.idDetail = detailId;
        this.manageService.getSettlement(this.idDetail)
            .subscribe({
                next: res => {
                    this.detailList = res.datas.goodsMsg;
                    console.log(this.detailList)
                    this.list.forEach((v, i) => {
                        if (v.id === this.idDetail) {
                            this.list[i].isSelect = true;
                        } else {
                            this.list[i].isSelect = false;
                        }
                    });

                },
                complete: () => this.loading = false
            });
    }
    // confirmDelete(goods: Goods) {
    //     this.confirm.danger('删除确认', `您确认删除商品'${goods.goodsName}'吗？`)
    //         .pipe(switchMap<void, ApiData>(() => this.manageService.deleteGoods(goods.id)))
    //         .subscribe(() => {
    //             this.list.splice(this.list.indexOf(goods), 1);
    //             this.toast.success('删除成功', `成功删除商品'${goods.goodsName}`);
    //             this.loadDatas();
    //         });
    // }



}
